
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# DISCLAIMER AND GENERAL INFORMATION
#
# File: App_C_order_effects.R
# Purpose: Appendix file for testing for order effects
# Date: 10 July 2024
# Data: Survey data (pulled through 0_cbam_prep.R)
#
# Technical disclaimer:
# All analyses in R version 4.4.1 (2024-06-14 ucrt) -- "Race for Your Life"
# R Studio 2024.04.2 Build 764 ("Chocolate Cosmos" Release (e4392fc9, 2024-06-05) for Windows)
# Windows 10 Enterprise, 64-bit
# 12th Gen Intel(R) Core(TM) i7-1255U 1.70 GHz with 16GB RAM
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (A) Load data and packages ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Run source file to load data
source("0_cbam_prep.R")

# Load packages
library("tidyverse")
library("janitor")
library("ggpubr")
library("modelsummary")
options(modelsummary_format_numeric_latex = "plain")
library("marginaleffects")
library("xtable")
library("tinytable")

# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (B) Analysis of order effects for compound treatments ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Check for order effects for compound treatments
table(dta$orderID, dta$tr, dta$country)

# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (B.1) DV: CBAM support ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Full data 
t.test(cbam_support~orderID, data=dta)$p.value

v1 <- as.numeric(table(dta$cbam_support[dta$orderID==1]))
v2 <- as.numeric(table(dta$cbam_support[dta$orderID==2]))

V <- data.frame(v1,v2)
chisq.test(V)$p.value


# t-tests for control group
p.val.t.ctrl <- c(t.test(cbam_support~orderID, data=dta[(dta$tr==4 | dta$tr==5) & dta$country=="germany",])$p.value,
                  t.test(cbam_support~orderID, data=dta[(dta$tr==4 | dta$tr==5) & dta$country=="hungary",])$p.value,
                  t.test(cbam_support~orderID, data=dta[(dta$tr==4 | dta$tr==5) & dta$country=="switzerland",])$p.value,
                  t.test(cbam_support~orderID, data=dta[(dta$tr==4 | dta$tr==5) & dta$country=="uk",])$p.value
)

# t-tests for trade group
p.val.t.trade <- c(t.test(cbam_support~orderID, data=dta[(dta$tr==9 | dta$tr==10) & dta$country=="germany",])$p.value,
                   t.test(cbam_support~orderID, data=dta[(dta$tr==9 | dta$tr==10) & dta$country=="hungary",])$p.value,
                   t.test(cbam_support~orderID, data=dta[(dta$tr==9 | dta$tr==10) & dta$country=="switzerland",])$p.value,
                   t.test(cbam_support~orderID, data=dta[(dta$tr==9 | dta$tr==10) & dta$country=="uk",])$p.value
)

# t-tests for climate frame
p.val.t.climate <- c(t.test(cbam_support~orderID, data=dta[(dta$tr==14 | dta$tr==15) & dta$country=="germany",])$p.value,
                     t.test(cbam_support~orderID, data=dta[(dta$tr==14 | dta$tr==15) & dta$country=="hungary",])$p.value,
                     t.test(cbam_support~orderID, data=dta[(dta$tr==14 | dta$tr==15) & dta$country=="switzerland",])$p.value,
                     t.test(cbam_support~orderID, data=dta[(dta$tr==14 | dta$tr==15) & dta$country=="uk",])$p.value
)



# chi-square tests for control frame

ktry <- c("germany", "hungary", "switzerland", "uk")
OUT <- list()

for (i in 1:4){

v1 <- as.numeric(table(dta$cbam_support[dta$tr==4 & dta$country==ktry[i]]))
v2 <- as.numeric(table(dta$cbam_support[dta$tr==5 & dta$country==ktry[i]]))

V <- data.frame(v1,v2)
OUT[[i]] <- chisq.test(V,simulate.p.value=TRUE)$p.value
}

p.val.chi.ctrl <- do.call(rbind,OUT)



# chi-square tests for trade frame

for (i in 1:4){
  
v1 <- as.numeric(table(dta$cbam_support[dta$tr==9 & dta$country==ktry[i]]))
v2 <- as.numeric(table(dta$cbam_support[dta$tr==10 & dta$country==ktry[i]]))

V <- data.frame(v1,v2)
OUT[[i]] <- chisq.test(V,simulate.p.value=TRUE)$p.value
}

p.val.chi.trade <- do.call(rbind,OUT)

# chi-square tests for climate frame

for (i in 1:4){

v1 <- as.numeric(table(dta$cbam_support[dta$tr==14 & dta$country==ktry[i]]))
v2 <- as.numeric(table(dta$cbam_support[dta$tr==15 & dta$country==ktry[i]]))

V <- data.frame(v1,v2)
OUT[[i]] <- chisq.test(V,simulate.p.value=TRUE)$p.value
}

p.val.chi.climate <- do.call(rbind,OUT)

row.names <- c("Germany", "Hungary", "Switzerland", "United Kingdom")

# Table C.1
table.support <- data.frame(p.val.t.ctrl,p.val.chi.ctrl, p.val.t.trade,p.val.chi.trade,p.val.t.climate,p.val.chi.climate,
                            row.names=row.names)
p <- xtable(table.support, label="app:order_support", caption="Test statistic p-values for order effects")
print.xtable(p, booktabs=TRUE, caption.placement="top")


# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (B.2) DV: Tradeoff ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Full data 
t.test(tradeoff~orderID, data=dta)$p.value

v1 <- as.numeric(table(dta$tradeoff[dta$orderID==1]))
v2 <- as.numeric(table(dta$tradeoff[dta$orderID==2]))

V <- data.frame(v1,v2)
chisq.test(V)$p.value



# t-tests for control group
p.val.t.ctrl <- c(t.test(tradeoff~orderID, data=dta[(dta$tr==4 | dta$tr==5) & dta$country=="germany",])$p.value,
                  t.test(tradeoff~orderID, data=dta[(dta$tr==4 | dta$tr==5) & dta$country=="hungary",])$p.value,
                  t.test(tradeoff~orderID, data=dta[(dta$tr==4 | dta$tr==5) & dta$country=="switzerland",])$p.value,
                  t.test(tradeoff~orderID, data=dta[(dta$tr==4 | dta$tr==5) & dta$country=="uk",])$p.value
)

# t-tests for trade group
p.val.t.trade <- c(t.test(tradeoff~orderID, data=dta[(dta$tr==9 | dta$tr==10) & dta$country=="germany",])$p.value,
                   t.test(tradeoff~orderID, data=dta[(dta$tr==9 | dta$tr==10) & dta$country=="hungary",])$p.value,
                   t.test(tradeoff~orderID, data=dta[(dta$tr==9 | dta$tr==10) & dta$country=="switzerland",])$p.value,
                   t.test(tradeoff~orderID, data=dta[(dta$tr==9 | dta$tr==10) & dta$country=="uk",])$p.value
)

# t-tests for climate frame
p.val.t.climate <- c(t.test(tradeoff~orderID, data=dta[(dta$tr==14 | dta$tr==15) & dta$country=="germany",])$p.value,
                     t.test(tradeoff~orderID, data=dta[(dta$tr==14 | dta$tr==15) & dta$country=="hungary",])$p.value,
                     t.test(tradeoff~orderID, data=dta[(dta$tr==14 | dta$tr==15) & dta$country=="switzerland",])$p.value,
                     t.test(tradeoff~orderID, data=dta[(dta$tr==14 | dta$tr==15) & dta$country=="uk",])$p.value
)


# chi-square tests for control frame
ktry <- c("germany", "hungary", "switzerland", "uk")
OUT <- list()

for (i in 1:4){
  
  v1 <- as.numeric(table(dta$tradeoff[dta$tr==4 & dta$country==ktry[i]]))
  v2 <- as.numeric(table(dta$tradeoff[dta$tr==5 & dta$country==ktry[i]]))
  
  V <- data.frame(v1,v2)
  OUT[[i]] <- chisq.test(V,simulate.p.value=TRUE)$p.value
}

p.val.chi.ctrl <- do.call(rbind,OUT)



# chi-square tests for trade frame

for (i in 1:4){
  
  v1 <- as.numeric(table(dta$tradeoff[dta$tr==9 & dta$country==ktry[i]]))
  v2 <- as.numeric(table(dta$tradeoff[dta$tr==10 & dta$country==ktry[i]]))
  
  V <- data.frame(v1,v2)
  OUT[[i]] <- chisq.test(V,simulate.p.value=TRUE)$p.value
}

p.val.chi.trade <- do.call(rbind,OUT)

# chi-square tests for climate frame

for (i in 1:4){
  
  v1 <- as.numeric(table(dta$tradeoff[dta$tr==14 & dta$country==ktry[i]]))
  v2 <- as.numeric(table(dta$tradeoff[dta$tr==15 & dta$country==ktry[i]]))
  
  V <- data.frame(v1,v2)
  OUT[[i]] <- chisq.test(V,simulate.p.value=TRUE)$p.value
}

p.val.chi.climate <- do.call(rbind,OUT)

row.names <- c("Germany", "Hungary", "Switzerland", "United Kingdom")

# Table C.2
table.tradeoff <- data.frame(p.val.t.ctrl,p.val.chi.ctrl,p.val.t.trade,p.val.chi.trade,p.val.t.climate,p.val.chi.climate,
                             row.names=row.names)
p <- xtable(table.tradeoff, label="app:order_tradeoff", caption="Test statistic p-values for order effects")
print.xtable(p, booktabs=TRUE, caption.placement="top")


# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                         END OF FILE
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
